<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/03/22
//      |_|                                         | TpMeCMS

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\user;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;


/**
 * 充值
 * @Auth(name="token",nologin={"no"})
 * @Controller("/user.recharge/")
 */
class Recharge extends UserBean
{
    protected $noNeedLogin = ['no'];
    protected $noNeedRight = ['*'];

    /**
     * 获取充值积分价格列表
     *
     * @return void
     * @Action
     */
    public function get_recharge_score_price_list()
    {

        return $this->success('', Db::name('recharge_score')->where('id', '>=', 1)->select()->getArray());
    }



    /**
     * 获取充值贝壳价格列表
     *
     * @return void
     * @Action
     */
    public function get_recharge_shell_price_list()
    {

        return $this->success('', Db::name('recharge_shell')->where('id', '>=', 1)->select()->getArray());
    }

    /**
     * 充值积分
     * @param int $id 充值id
     * @param float $score 充值积分
     * @param string $pay_type 支付类型:1=微信,2=支付宝,3=钱包,9=其他
     * @param string $pay_password 支付密码
     * @return void
     * @Action
     */
    public function recharge_score($id = null,?float $score = null, $pay_type = null, $pay_password = null)
    {
        if (!$pay_type)
            $pay_type = '3';

        if($pay_type=='3'){
            //验证密码 
            $password = $this->VerifyUserPayPassword($this->auth->user()->id, $pay_password);
            if ($password != 'ok') {
                return $this->error('支付密码错误', null, CODE_用户支付密码错误);
            }
        }
        $shell = 0;
        if ($id) {
            $res = Db::name('recharge_score')->where('id', '=', $id)->find();
            if (!$res) {
                return $this->error('没有该充值规则');
            }
            $score = (float)$res['score'];
            $shell = (float)$res['shell'];
            $money = (float)$res['money'];
        }else{
            $money = $score;
        }

        switch ($pay_type) {
            case '1':
                # code...
                break;
            case '2':
                # code...
                break;
            case '3':
                if ($this->auth->money() < $money) {
                    return $this->error('余额不足', $money, CODE_余额不足);
                }
                //减余额
                $this->DecUserMoney($this->auth->user()->id, $money, '充值积分', 33, 0);

                $this->IncUserScore($this->auth->user()->id, $score, '充值积分', 33, 0);
                if ($shell > 0)
                    $this->IncUserShell($this->auth->user()->id, $shell, '充值积分,送贝壳', 33, 0);

                return $this->success('充值成功');


                break;
            default:
                # code...
                break;
        }


        return $this->error('暂无该方式充值');
    }




    /**
     * 充值贝壳
     * @param int $id 充值id
     * @param float $shell 充值贝壳
     * @param string $pay_type 支付类型:1=微信,2=支付宝,3=钱包,9=其他
     * @param string $pay_password 支付密码
     * @return void
     * @Action
     */
    public function recharge_shell($id = null,?float $shell = null, $pay_type = null, $pay_password = null)
    {

        if (!$pay_type)
            $pay_type = '3';
        if($pay_type=='3'){
            //验证密码 
            $password = $this->VerifyUserPayPassword($this->auth->user()->id, $pay_password);
            if ($password != 'ok') {
                return $this->error('支付密码错误', null, CODE_用户支付密码错误);
            }
        }
        $score = 0;
        if ($id) {
            $res = Db::name('recharge_shell')->where('id', '=', $id)->find();
            if (!$res) {
                return $this->error('没有该充值规则');
            }
            $shell = (float)$res['shell'];
            $score = (float)$res['score'];
            $money = (float)$res['money'];
        }else{
            $money = $shell;
        }

        switch ($pay_type) {
            case '1':
                # code...
                break;
            case '2':
                # code...
                break;
            case '3':
                if ($this->auth->money() < $money) {
                    return $this->error('余额不足', $money, CODE_余额不足);
                }
                //减余额
                $this->DecUserMoney($this->auth->user()->id, $money, '充值贝壳', 34, 0);

                $this->IncUserShell($this->auth->user()->id, $shell, '充值贝壳', 34, 0);
                if ($score > 0)
                    $this->IncUserScore($this->auth->user()->id, $score, '充值贝壳,送积分', 34, 0);

                return $this->success('充值成功');


                break;
            default:
                # code...
                break;
        }


        return $this->error('暂无该方式充值');
    }
}
